using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;

namespace Xivo
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      if (isOnlyInstance()) {
        // later, we need to expand this to dynamically load radio modules
        // based on what we're using
        Xivo.Shared.Common.Radio = new Xivo.Shared.XM();
        Application.Run(new MainForm());
      }
    }

    static bool isOnlyInstance()
    {
      Process[] appProc;
      Process me;
      string modName, procName;

      // switch to the existing Xivo task if one is open.
      // otherwise, open the main window.
      me = Process.GetCurrentProcess();
      modName = me.MainModule.ModuleName;
      procName = System.IO.Path.GetFileNameWithoutExtension(modName);
      appProc = Process.GetProcessesByName(procName);
      if (appProc.Length > 1)
      {
        foreach (Process proc in appProc)
        {
          if (proc.Id != Process.GetCurrentProcess().Id)
            Interaction.AppActivate(proc.Id);
        }
        return false;
      }
      return true;
    }
  }
}